package org.cttic.visualization.visualization.z_service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.client.FindIterable;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.cttic.visualization.visualization.common.EsDataIncomplete;
import org.cttic.visualization.visualization.common.EsDataStrip;
import org.cttic.visualization.visualization.common.commonConf;
import org.cttic.visualization.visualization.common.itineraryQueryData;
import org.cttic.visualization.visualization.util.ConvertColor;
import org.cttic.visualization.visualization.util.DataUtil;
import org.cttic.visualization.visualization.util.DateUtil;
import org.cttic.visualization.visualization.util.ParseJsonDataUtil;
import org.cttic.visualization.visualization.util.itineraryQuery.queryUtil;
import org.cttic.visualization.visualization.util.itineraryQuery.tripUtil;
import org.cttic.visualization.visualization.z_service.EmployeeRepository;
import org.cttic.visualization.visualization.z_service.z_ItineraryQuery;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author shkstart
 * @create 2020-05-07 9:28
 * 2.3.1车辆行程查询接口
 */
@Service
public class z_ItineraryQueryImpl implements z_ItineraryQuery {
    @Autowired
    private queryUtil query;
    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    private tripUtil tripUtil;
    @Autowired
    private EmployeeRepository employeeRepository;
    @Override
    public String itineraryQuery(String inputData) {
        JSONObject jsonData = ParseJsonDataUtil.getJsonData(inputData);
        String provinceId = jsonData.getString("provinceId");
        String vehPlate = jsonData.getString("vehPlate");
        String queryType = jsonData.getString("queryType");
        String vehPlateColor = jsonData.getString("vehPlateColor");
        String feeFlag = jsonData.getString("feeFlag");
        String queryValue1 = jsonData.getString("queryValue1");
        itineraryQueryData itineraryQueryData = new itineraryQueryData();

        itineraryQueryData.setPageNo(1);
        itineraryQueryData.setPageSize(1);
        itineraryQueryData.setTotalCount(1);
        itineraryQueryData.setTotalPage(1);
        List<itineraryQueryData.Trip> list = new ArrayList<>();
        //按全国查询
        //&&query.judgeProvinceId(vehPlate,vehPlateColor,provinceId)

            //按行程编码查询
            if(queryType.equals("2")){
                tripUtil.queryTrip(vehPlate,vehPlateColor,queryValue1,feeFlag,list,provinceId);
                itineraryQueryData.setTripList(list);
                ObjectMapper mapper = new ObjectMapper();
                try {
                    String json = mapper.writeValueAsString(itineraryQueryData);

                    return json;
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
            //按时间段查询
            }else if(queryType.equals("1")){
                System.out.println("********时间段查询*********");
                String queryValue2 = jsonData.getString("queryValue2");     //结束时间
                String startTime = DateUtil.TtoTime(queryValue1);
                String endTime = DateUtil.TtoTime(queryValue2);
//                String endLat=null;
                //完整行程
                SearchHit[] searchHits = query.queryByTansTimeBetween(vehPlate, vehPlateColor, startTime, endTime,provinceId);
                if(searchHits.length>0) {
                    SearchHit hit = searchHits[0];
                    Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                    Object travelId = sourceAsMap.get("travelId");
                    if(travelId!=null){
                        String value = String.valueOf(travelId);
                        tripUtil.queryTrip(vehPlate,vehPlateColor,value,feeFlag,list,provinceId);
                    }

                }else {
                    //不完整行程

                    SearchHit[] searchHits2 = query.queryByTansTimeBetween2(vehPlate, vehPlateColor, startTime, endTime,provinceId);
                    if(searchHits2.length>0) {
                        SearchHit hit = searchHits2[0];
                        Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                        Object travelId = sourceAsMap.get("travelId");
                        if(travelId!=null){
                            String value = String.valueOf(travelId);
                            tripUtil.queryTrip(vehPlate,vehPlateColor,value,feeFlag,list,provinceId);
                        }
                    }
                }
                System.out.println("****************时间段查询结束*****************");
                itineraryQueryData.setTripList(list);
                ObjectMapper mapper = new ObjectMapper();
                try {
                    String json = mapper.writeValueAsString(itineraryQueryData);

                    return json;
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }

            //按时间点查询
        }else if(queryType.equals("0")){
            String Time = DateUtil.TtoTime(queryValue1);
                try {
                    String stamp = DateUtil.dateToStamp(Time);
                    String startTime = DateUtil.StampToDate(Long.parseLong(stamp)-3600000);
                    String endTime = DateUtil.StampToDate(Long.parseLong(stamp)+3600000);
                    //完整行程
                    SearchHit[] searchHits = query.queryByTansTimeBetween(vehPlate, vehPlateColor, startTime, endTime,provinceId);
                    System.out.println("****************时间点查询开始*****************");
                    if(searchHits.length>0) {
                        SearchHit hit = searchHits[0];
                        Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                        Object travelId = sourceAsMap.get("travelId");
                        if(travelId!=null){
                            String value = String.valueOf(travelId);
                            tripUtil.queryTrip(vehPlate,vehPlateColor,value,feeFlag,list,provinceId);
                        }
                    }else {
                        SearchHit[] searchHits2 = query.queryByTansTimeBetween2(vehPlate, vehPlateColor, startTime, endTime,provinceId);
                        if(searchHits2.length>0) {
                            SearchHit hit = searchHits2[0];
                            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                            Object travelId = sourceAsMap.get("travelId");
                            if(travelId!=null){
                                String value = String.valueOf(travelId);
                                tripUtil.queryTrip(vehPlate,vehPlateColor,value,feeFlag,list,provinceId);
                            }
                        }
                    }
                    System.out.println("****************时间点查询结束*****************");
                    itineraryQueryData.setTripList(list);
                    ObjectMapper mapper = new ObjectMapper();
                    try {
                        String json = mapper.writeValueAsString(itineraryQueryData);

                        return json;
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }

                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

        return null;
    }
}
